package com.yxq.mdt.app.modules.picturebrowse.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.PagerAdapter;

import com.luck.picture.lib.photoview.PhotoView;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.bottomsheet.SavePictureSheetDialog;
import com.yxq.mdt.app.modules.picturebrowse.bean.BasePictureBean;

import java.util.List;

/**
 * @author L
 * @time 2019-11-25
 * @describe 浏览大图$
 */
public class PicturePageAdapter extends PagerAdapter {
    private List<String> mBasePictureBeanList;
    private Context mContext;
    private List<PhotoView> mPhotoViewList;
    private PicturePageInterface picturePageInterface;
    private FragmentManager fragmentManager;
    String loadUrl = null;

    public PicturePageAdapter(Context context, FragmentManager fragmentManager, List<String> basePictureBeanList) {
        this.mContext = context;
        this.fragmentManager = fragmentManager;
        this.mBasePictureBeanList = basePictureBeanList;
    }

    @Override
    public int getCount() {
        return mBasePictureBeanList != null ? mBasePictureBeanList.size() : 0;
    }


    //判断是否是否为同一张图片，这里返回方法中的两个参数做比较就可以
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //设置viewpage内部东西的方法，如果viewpage内没有子空间滑动产生不了动画效果
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(mContext);
        String url = mBasePictureBeanList.get(position);
        //处理 截取原图链接 去掉参数
        int index = url.indexOf("?x-oss-process=");

        if (index > 0) {
            String data = url.substring(0, index);
            loadUrl = data;
            GlideHelper.setLoadImage(mContext, data, R.color.black, photoView);
        } else {
            loadUrl = url;
            GlideHelper.setLoadImage(mContext, url, R.color.black, photoView);
        }
        photoView.setOnPhotoTapListener((view, x, y) -> {
            if (picturePageInterface != null) {
                picturePageInterface.finish();
            }
        });
        photoView.setTag(loadUrl);
        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (v.getTag() == null) {
                    return true;
                }
                //长按保存图片
                SavePictureSheetDialog savePictureSheetDialog = new SavePictureSheetDialog(mContext, v.getTag() + "");
                savePictureSheetDialog.show(fragmentManager, "SavePictureSheetDialog");
                return false;
            }
        });
        container.addView(photoView);
        //最后要返回的是控件本身
        return photoView;
    }

    //因为它默认是看三张图片，第四张图片的时候就会报错，还有就是不要返回父类的作用
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //         super.destroyItem(container, position, object);
    }

    public interface PicturePageInterface {
        void finish();
    }

    public void setPicturePageInterface(PicturePageInterface picturePageInterface) {
        this.picturePageInterface = picturePageInterface;
    }
}
